﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class GSH : UserControl
    {
        public GSH()
        {
            InitializeComponent();
        }

        private void GSH_Load(object sender, EventArgs e)
        {

        }
        
        private void button2_Click_1(object sender, EventArgs e)
        {
            string zhi1 = textBox1.Text;
            string[] list1 = zhi1.Split(Environment.NewLine.ToCharArray());

            string boolStr = judgeOnly(list1);
            if (!string.IsNullOrEmpty(boolStr))
            {
                MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
                DialogResult dr = MessageBox.Show(boolStr + "，是否继续生成？", "发现重复", messButton);

                if (dr == DialogResult.OK)//如果点击“确定”按钮
                {
                    string jieguo1 = GetStr(list1);
                    textBox5.Text = jieguo1;
                }
                if (dr == DialogResult.Cancel)//如果点击“取消”按钮
                {

                    string[] chongfu = boolStr.Split(',');
                    string textbox5Str = "";
                    foreach (var item in chongfu)
                    {
                        if (!string.IsNullOrEmpty(item))
                        {
                            textbox5Str += "" + item + "\r\n";
                        }
                    }
                    textBox5.Text = textbox5Str;
                    return;
                }
                return;
            }
            
            string jieguo = GetStr(list1);
            textBox5.Text = jieguo;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string zhi1 = textBox1.Text;
            string[] list1 = zhi1.Split(Environment.NewLine.ToCharArray());

            string boolStr = judgeOnly(list1);
            if (!string.IsNullOrEmpty(boolStr))
            {
                MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
                DialogResult dr = MessageBox.Show(boolStr+"，是否继续生成？", "发现重复", messButton);

                if (dr == DialogResult.OK)//如果点击“确定”按钮
                {
                    string jieguo1 = GetStr(list1);
                    Clipboard.SetDataObject(jieguo1);
                    textBox5.Text = jieguo1;
                }
                if (dr == DialogResult.Cancel)//如果点击“取消”按钮
                {

                    string[] chongfu = boolStr.Split(',');
                    string textbox5Str = "";
                    foreach (var item in chongfu)
                    {
                        if (!string.IsNullOrEmpty(item))
                        {
                            textbox5Str += "" + item + "\r\n";
                        }
                    }
                    textBox5.Text = textbox5Str;
                    return;
                }
                return;
            }

            string jieguo = GetStr(list1);
            Clipboard.SetDataObject(jieguo);
            textBox5.Text = jieguo;
        }

        public string judgeOnly(string[] arr)
        {
            List<string> xinde = new List<string>();
            string jieguo = "";
            foreach (var item in arr)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    if (xinde.Exists(o => o.ToLower() == item.ToLower()))//包含
                    {
                        jieguo += item + ",";
                    }
                    else
                    {
                        xinde.Add(item);
                    }
                }
            }
            return jieguo;
        }

        public string GetStr(string[] list1)
        {
            try
            {
                
                //string zhi1 = textBox1.Text;
                string yuensuan = textBox2.Text;
                //string[] list1 = null;
                //list1 = zhi1.Split(Environment.NewLine.ToCharArray());
                
                string jieguo = "";
                if (list1.Length == 0 || yuensuan == "")
                {
                    return "";
                }

                string dan = "";

                for (int i = 0; i < list1.Length; i++)
                {
                    if (!string.IsNullOrWhiteSpace(list1[i]))
                    {

                        dan = yuensuan;
                        dan = dan.Replace("[val]", list1[i]);
                        dan = dan.Replace("[val1]", list1[i]);
                        dan = dan.Replace("'", "");

                        if (checkBox2.Checked)
                        {
                            dan = dan.Replace(" ", "");
                        }

                        if (radioButton1.Checked)//大写
                        {
                            if (checkBox3.Checked)
                            {
                                if (dan.Substring(0, 1) == "[")
                                {
                                    dan = "[" + dan.Substring(1, 1).ToUpper() + dan.Substring(2);
                                }
                                else
                                {
                                    dan = dan.Substring(0, 1).ToUpper() + dan.Substring(1);
                                }
                            }
                            else
                            {
                                dan = dan.ToUpper();
                            }
                        }
                        if (radioButton2.Checked)//小写
                        {
                            if (checkBox3.Checked)
                            {
                                if (dan.Substring(0, 1) == "[")
                                {
                                    dan = "[" + dan.Substring(1, 1).ToLower() + dan.Substring(2);
                                }
                                else
                                {
                                    dan = dan.Substring(0, 1).ToLower() + dan.Substring(1);
                                }
                            }
                            else
                            {
                                dan = dan.ToLower();
                            }
                        }
                        
                        jieguo += dan;
                        dan = "";

                    }
                }
                return jieguo;
                
            }
            catch
            {
                MessageBox.Show("信息输入有误！");
                return "";
            }
        }

        private void textBox3_Click(object sender, EventArgs e)
        {
            textBox3.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox6.Text = "";
            int geshu = int.Parse( textBox3.Text);
            string zhi = textBox4.Text;

            for (int i = 0; i < geshu; i++)
            {
                textBox6.Text += zhi+"\r\n";
            }

        }

        private void textBox4_Click(object sender, EventArgs e)
        {
            textBox4.Text = "";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var buchongfu = new List<string>();

            string zhi1 = textBox1.Text;
            textBox5.Text = "";

            string[] list1 = zhi1.Split(Environment.NewLine.ToCharArray());
            for (int i = 0; i < list1.Length; i++)
            {
                if (!string.IsNullOrWhiteSpace(list1[i]))
                {
                    if (buchongfu.Where(a => a == list1[i]).Count() == 0)
                    {
                        buchongfu.Add(list1[i]);
                        textBox5.Text += list1[i]+"\r\n";
                    }
                }
            }
        }

        private void textBox1_DoubleClick(object sender, EventArgs e)
        {
            textBox1.Focus();
            textBox1.SelectAll();
        }

        private void textBox5_DoubleClick(object sender, EventArgs e)
        {
            textBox5.Focus();
            textBox5.SelectAll();
        }

        private void textBox6_DoubleClick(object sender, EventArgs e)
        {
            textBox6.Focus();
            textBox6.SelectAll();
        }

        private void textBox2_DoubleClick(object sender, EventArgs e)
        {
            textBox2.Focus();
            textBox2.SelectAll();
        }
    }
}
